package cn.damong.utils;

import java.util.Random;

public class WeixinUtil {
    private static final Random random = new Random();
    
    /**
     * 将字节数组转换为十六进制字符串
     * 
     * @param byteArray
     * @return
     */
    public static String byteToStr(byte[] byteArray) {
        String strDigest = "";
        for (int i = 0; i < byteArray.length; i++) {
            strDigest += byteToHexStr(byteArray[i]);
        }
        return strDigest;
    }

    /**
     * 将字节转换为十六进制字符串
     * 
     * @param mByte
     * @return
     **/
    public static String byteToHexStr(byte mByte) {
        char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
        char[] tempArr = new char[2];
        tempArr[0] = Digit[(mByte >>> 4) & 0X0F];
        tempArr[1] = Digit[mByte & 0X0F];

        String s = new String(tempArr);
        return s;
    }
    
    /**
     * 获取长度为size的随机字符串，随机字符串由数字0-9和小写字母组成
     * 
     * @param size 需要传入大于0的正整数
     * @return
     */
    public static String getNonceString(int size) {
        if(size <= 0) {
            size = 10;
        }
        String source = "0123456789qwertyuiopasdfghjklzxcvbnm";
        
        StringBuffer sb = new StringBuffer();
        for(int i = 0 ; i < size; i++) {
            int index = random.nextInt(source.length());
            sb.append(source.charAt(index));
        }
        return sb.toString();
    }
    
    public static Long getTimestamp() {
        return System.currentTimeMillis() / 1000;
    }
}
